﻿using System;
using System.IO;
using System.Net.Mail;

namespace SimpleServiceBus.Utilities.Email
{
    public class SmtpEmailService : IEmailService
    {
        public SmtpEmailService()
        {
            ServerName = "localhost";
            PortNumber = 25;
            FromAddress = "ssb_notification@noreply.com";
        }

        public string ServerName { get; set; }
        public int PortNumber { get; set; }
        public string FromAddress { get; set; }

        public void Send(string subject, string message, string fileName, byte[] file, params string[] recipients)
        {
            var smtp = new SmtpClient(ServerName, PortNumber);
            var email = new MailMessage
            {
                Subject = subject,
                Body = message,
                From = new MailAddress(FromAddress)
            };

            if (!string.IsNullOrEmpty(fileName) && file != null && file.Length > 0)
            {
                email.Attachments.Add(new Attachment(new MemoryStream(file), fileName));
            }

            foreach (string recipient in recipients)
                email.To.Add(new MailAddress(recipient));

            smtp.Send(email);
        }

        public void Send(string subject, string message, params string[] recipients)
        {
            Send(subject, message, null, null, recipients);
        }
    }
}
